package com.fxu.tpl.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fxu.framework.core.base.Cascade;
import com.fxu.framework.core.enums.EnumValid;
import com.fxu.framework.core.enums.StatusEnum;
import com.fxu.framework.core.sql.SEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import static com.baomidou.mybatisplus.annotation.FieldFill.UPDATE;

/***
 * <p>fxu</p>
 * [table]的数据实体类
 * @author fangxu
 * @version 2019-09-23
 ***/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "Table", description = "表")
@TableName("fxu_table")
public class Table extends SEntity<Table> implements Serializable {
/*=== 自动生成代码 开始 ===*/
    private static final long serialVersionUID = -1431548916965075158L;

    @TableId(type = IdType.AUTO)
    @ApiModelProperty(value = "自增ID")
    private Long id;

    @ApiModelProperty(value = "所属项目ID", required = true)
    @NotNull(message = "所属项目ID必须赋值")
    private Long projectId;

    @ApiModelProperty(value = "所属模块ID", required = true)
    @NotNull(message = "所属模块ID必须赋值")
    private Long moduleId;

    @ApiModelProperty(value = "表名", required = true)
    @NotBlank(message = "表名必须赋值")
    @Size(max = 64, message = "表名的长度必须小于64位")
    private String name;

    @ApiModelProperty(value = "表注释", required = true)
    @NotBlank(message = "表注释必须赋值")
    @Size(max = 128, message = "表注释的长度必须小于128位")
    private String cmmt;

    @ApiModelProperty(value = "分栏布局", required = true)
    @NotNull(message = "分栏布局必须赋值")
    private Integer rowCol;

    @ApiModelProperty(value = "生产文件")
    @Size(max = 128, message = "生产文件的长度必须小于128位")
    private String genFiles;

    @ApiModelProperty(value = "树形展示")
    private Boolean isTree;

    @ApiModelProperty(value = "权限控制")
    private Boolean isPerm;

    @ApiModelProperty(value = "是否导入", required = true)
    @NotNull(message = "是否导入必须赋值")
    private Boolean isImport;

    @ApiModelProperty(value = "是否导出", required = true)
    @NotNull(message = "是否导出必须赋值")
    private Boolean isExport;

    @ApiModelProperty(value = "是否自增ID", required = true)
    @NotNull(message = "是否自增ID必须赋值")
    private Boolean isAutoId;

    @ApiModelProperty(value = "是否有操作")
    private Boolean hasAction;

    @ApiModelProperty(value = "显示字段")
    private String showCln;

    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    @ApiModelProperty(value = "修改时间")
    private Date updateTime;

    @ApiModelProperty(value = "状态[1:正常,0:删除]")
    @EnumValid(target = StatusEnum.class, message = "状态的值必须为{1:正常,0:删除}")
    private Integer status;

    @TableField(fill = UPDATE, update = "%s+1")
    @ApiModelProperty(value = "版本号")
    private Integer version;

    @TableField(exist = false)
    @ApiModelProperty(value = "所属项目对象")
    @Cascade(thisField = "projectId", linkField = "id")
    private Project project;

    @TableField(exist = false)
    @ApiModelProperty(value = "所属模块对象")
    @Cascade(thisField = "moduleId", linkField = "id")
    private Module module;

    @Valid
    @TableField(exist = false)
    @ApiModelProperty(value = "字段")
    @Cascade(thisField = "id", linkField = "tableId", save = true)
    private List<Column> columnList;

    @Valid
    @TableField(exist = false)
    @ApiModelProperty(value = "索引")
    @Cascade(thisField = "id", linkField = "tableId", save = true)
    private List<Index> indexList;

    @Valid
    @TableField(exist = false)
    @ApiModelProperty(value = "一对多")
    @Cascade(thisField = "id", linkField = "tableId", save = true)
    private List<Link> linkList;

    /** 获得状态的说明 */
    public String getStatusText() {
        return StatusEnum.desc(status);
    }
/*=== 自动生成代码 结束 ===*/

    @TableField(exist = false)
    @ApiModelProperty(value = "有创建者字段")
    private boolean hasStatus = false;

    @TableField(exist = false)
    @ApiModelProperty(value = "有修改者字段")
    private boolean hasVersion = false;

    @TableField(exist = false)
    @ApiModelProperty(value = "有创建者字段")
    private boolean hasCreateBy = false;

    @TableField(exist = false)
    @ApiModelProperty(value = "有修改者字段")
    private boolean hasUpdateBy = false;

    @TableField(exist = false)
    @ApiModelProperty(value = "有修改时间字段")
    private boolean hasUpdateTime = false;

    @TableField(exist = false)
    @ApiModelProperty(value = "项目名")
    private String projectName;

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Table other = (Table) obj;
        if (cmmt == null) {
            if (other.cmmt != null) {
                return false;
            }
        } else if (!cmmt.equals(other.cmmt)) {
            return false;
        }
        if (columnList == null) {
            if (other.columnList != null) {
                return false;
            }
        } else if (!columnList.equals(other.columnList)) {
            return false;
        }
        if (indexList == null) {
            if (other.indexList != null) {
                return false;
            }
        } else if (!indexList.equals(other.indexList)) {
            return false;
        }
        if (linkList == null) {
            if (other.linkList != null) {
                return false;
            }
        } else if (!linkList.equals(other.linkList)) {
            return false;
        }
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((cmmt == null) ? 0 : cmmt.hashCode());
        result = prime * result + ((columnList == null) ? 0 : columnList.hashCode());
        result = prime * result + ((indexList == null) ? 0 : indexList.hashCode());
        result = prime * result + ((linkList == null) ? 0 : linkList.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
}
